#! /usr/bin/env python
# encoding: utf-8
# Calle Rosenquist, 2016 (xbreak)

"""
Execute Python tests during build

To force all tests, run with "waf build --alltests"
"""

top = '.'
out = 'build'

def test_results(bld):
	"""
	Custom post- function that prints out test results.
	"""
	lst = getattr(bld, 'utest_results', [])
	if not lst:
		return
	for (f, code, out, err) in lst:
		print(out)
		print(err)


def options(opt):
	opt.load('python')
	opt.load('waf_unit_test')

def configure(cnf):
	cnf.load('python')
	cnf.load('pytest')
	cnf.load('waf_unit_test')
	cnf.check_python_version(minver=(2, 5, 0))

def build(bld):
	# Python package under test
	bld(name         = 'foo',
		features     = 'py',
		source       = bld.path.ant_glob('src/**/*.py'),
		install_from = 'src')

	# Unit test example using the built in module unittest and let that discover
	# any test cases.
	# By using ``foo`` the **install_path** for ``foo`` will be automatically
	# added to sys.path via PYTHONPATH.
	bld(features      = 'pytest',
		use           = 'foo',
		pytest_source = bld.path.ant_glob('test/*.py'),
		ut_str        = '${PYTHON} -B -m unittest discover')

	bld.add_post_fun(test_results)
